package com.qdu.study_app.fragment;

import androidx.lifecycle.ViewModelProvider;

import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.navigation.ui.NavigationUI;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.qdu.study_app.Activity.Activity_clock;
import com.qdu.study_app.Activity.Activity_new_clock;
import com.qdu.study_app.Adapter.MyRecyclerViewAdapter;
import com.qdu.study_app.R;
import com.qdu.study_app.bean.Clock;
import com.qdu.study_app.bean.StudyItem;
import com.qdu.study_app.bean.User;
import com.qdu.study_app.utils.MyToast;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;

public class StudyFragment extends Fragment {
    // 定义view用来设置Fragment的layout
    private View view;
    private StudyViewModel mViewModel;
    private RecyclerView mRecycleView;
    private List<Clock> studyItemList = new ArrayList<>();
    private MyRecyclerViewAdapter mAdapter;
    private LinearLayoutManager mlinearLayoutManager;

    private ScrollView task_scroll;

    private FloatingActionButton addTaskBtn;

    public static final String TASK_ID = "com.qdu.study_app.TASK_ID";
    public static final String TASK_TIME = "com.qdu.study_app.TASK_TIME";
    public static final String TASK_IMG = "com.qdu.study_app.TASK_IMG";

    public static final int[] imgList = {
            R.drawable.ic_img1,
            R.drawable.ic_img2,
            R.drawable.ic_img3,
            R.drawable.ic_img4,
            R.drawable.ic_img5,
            R.drawable.ic_img6,
            R.drawable.ic_img7,
            R.drawable.ic_img8,
            R.drawable.ic_img9,
            R.drawable.ic_img10
    };


    public static final String USER_ID = "com.qdu.study_app.USER_ID";
    public static StudyFragment newInstance() {
        return new StudyFragment();
    }


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.study_fragment,container,false);
        addTaskBtn = view.findViewById(R.id.floatingActionButton);
        addTaskBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getContext(), Activity_new_clock.class);
                startActivity(intent);
            }
        });
        return view;

    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mViewModel = new ViewModelProvider(this).get(StudyViewModel.class);
        // TODO: Use the ViewModel
    }

    @Override
    public void onStart() {
        super.onStart();
        System.out.println("study_f_start:-----------------------------------");
    }

    @Override
    public void onResume() {
        super.onResume();
        System.out.println("study_f_resume:-----------------------------------");
        initData();
    }

    private void initData(){
        // 初始化数据
        BmobQuery<Clock> bmobQuery = new BmobQuery<>();

        bmobQuery.findObjects(new FindListener<Clock>() {
            @Override
            public void done(List<Clock> list, BmobException e) {
                if(e == null){
                    studyItemList.clear();
                    for(Clock c:list){
                        studyItemList.add(c);
                    }
                    initRecyclerView();

                }else{
                    System.out.println("查询所有clock失败：----------------");
                }
            }
        });
        //studyItemList.add(new StudyItem("30分钟","数学", R.drawable.ic_img1));
        //studyItemList.add(new StudyItem("40分钟","英语", R.drawable.c_img4));

    }
    private void initRecyclerView(){
        // 绑定滚动条
      //  task_scroll = (ScrollView) view.findViewById(R.id.task_scroll);
        mRecycleView = view.findViewById(R.id.study_list);

        //设置layoutManager,可以设置显示效果，是线性布局、grid布局，还是瀑布流布局
        //参数是：上下文、列表方向（横向还是纵向）、是否倒叙
        mlinearLayoutManager = new LinearLayoutManager(getActivity(),LinearLayoutManager.VERTICAL,false);
        mAdapter = new MyRecyclerViewAdapter(getActivity(),studyItemList);
        mAdapter.setOnItemClickListener(new MyRecyclerViewAdapter.OnItemClickListener(){
            @Override
            public void onItemClick(View view, int position) {
                //MyToast.showToast(getContext(),"点击了"+studyItemList.get(position), Toast.LENGTH_SHORT);
                Clock clock = studyItemList.get(position);
                BmobUser user = BmobUser.getCurrentUser();
                Intent intent = new Intent(getContext(), Activity_clock.class);
                TextView textView = (TextView)view;
                String time = (String)textView.getText();
                time = time.split("分")[0];
                // 传递设置的总时间
                intent.putExtra("time",time);
                // 传递开启任务的用户信息
                intent.putExtra(StudyFragment.USER_ID,user.getObjectId());
                intent.putExtra(StudyFragment.TASK_ID,clock.getObjectId());
                intent.putExtra(StudyFragment.TASK_TIME,String.valueOf(clock.getTime()));
                intent.putExtra(StudyFragment.TASK_IMG,String.valueOf(clock.getImg_index()));
                startActivity(intent);
            }
        });
        mRecycleView.setLayoutManager(mlinearLayoutManager);
        mRecycleView.setAdapter(mAdapter);

    }
}